package com.yituo.mjworld.mvc.action.user;

import com.yituo.mjworld.core.annotation.Auth;
import com.yituo.mjworld.core.annotation.Ban;
import com.yituo.mjworld.core.annotation.Check;
import com.yituo.mjworld.core.exception.MyException;
import com.yituo.mjworld.core.web.core.MyHttpRequest;
import com.yituo.mjworld.core.web.core.MyHttpResponse;
import com.yituo.mjworld.core.web.core.Result;
import com.yituo.mjworld.mvc.action.IBaseAction;
import com.yituo.mjworld.mvc.entity.web.Product;
import com.yituo.mjworld.mvc.entity.web.UserRole;
import com.yituo.mjworld.mvc.entity.web.WebShopCut;
import com.yituo.mjworld.mvc.entity.web.WebUser;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.service.impl.OrderServiceImpl;
import com.yituo.mjworld.mvc.service.impl.ProductServiceImpl;
import com.yituo.mjworld.mvc.service.impl.ShopCatServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * 购物车模块
 */
@Component
public class ShopAction implements IBaseAction {
  @Autowired
  ShopCatServiceImpl shopCatService;

  @Autowired
  ProductServiceImpl productService;

  @Autowired
  OrderServiceImpl orderService;

  /**
   * 管理员和普通用户均可查看
   *
   * @param req
   * @param res
   * @throws IOException
   */
  @Override
  @Auth(all = true)
  @Check(form = QueryForm.class)
  public void getList(MyHttpRequest req, MyHttpResponse res) throws IOException {
    WebUser user = req.getCurrentUser();
    System.out.println("head" + req.getHeader("token"));
    if (UserRole.isSuperAdmin(user.getRule())) {
      // 请求的用户为管理员
      res.success(shopCatService.getList((QueryForm) req.getForm()));
    } else {
      // 请求的用户为普通用户
      res.success(shopCatService.getListForUser((QueryForm) req.getForm(), user.getId()));
    }
  }

  /**
   * 除匿名用户以外均可访问
   *
   * @param req
   * @param res
   * @throws IOException
   */
  @Override
  @Auth(admin_user = true)
  @Check(form = DeleteForm.class)
  public void delete(MyHttpRequest req, MyHttpResponse res) throws IOException {
    WebUser user = req.getCurrentUser();
    if (UserRole.isSuperAdmin(user.getRule())) {
      // 请求的用户为管理员
      res.setResult(shopCatService.delete((DeleteForm) req.getForm()));
    } else {
      // 请求的用户为普通用户
      res.setResult(shopCatService.deleteForUser((DeleteForm) req.getForm(), user.getId()));
    }
  }

  @Ban
  @Override
  public void saveOrUpdate(MyHttpRequest req, MyHttpResponse res) throws IOException, InvocationTargetException, IllegalAccessException {

  }

  /**
   * 保存或更新.
   * <p>
   *
   * @param req
   * @param res
   * @throws IOException
   */
  @Auth(admin_user = true)
  @Check(form = WebShopCut.class)
  public void add2ShopCat(MyHttpRequest req, MyHttpResponse res) throws IOException {
    WebShopCut shopCut = (WebShopCut) req.getForm();
    Product product = productService.getProduct(shopCut.getProductId());
    if (product == null) {
      res.failed("产品不存在!");
      return;
    }
    WebUser user = req.getCurrentUser();
    if (!UserRole.isSuperAdmin(user.getRule())) {
      shopCut.setUserId(user.getId());
    }
    // 验证价格,没有遭遇修改就置入数据库
    Result result = orderService.validatePrice(product, shopCut.getTemplate(), shopCut.getNum(), shopCut.getFinalPrice(), shopCut.getTotalPrice());
    if (result.getResult()) {
      // 加入购物车
      res.setResult(shopCatService.add2ShopCat((WebShopCut) req.getForm(), req.getCurrentUser().getId()));
    } else {
      res.failed(result.getMsg().toString());
    }
  }

  /**
   * 更新数量
   * <p>
   *
   * @param req
   * @param res
   * @throws IOException
   */
  @Auth(admin_user = true)
  public void updateNum(MyHttpRequest req, MyHttpResponse res) throws IOException {
    int recordId = -1;
    int num = -1;
    try {
      recordId = Integer.parseInt(req.getParameter("id"));
      num = Integer.parseInt(req.getParameter("num"));
      if (recordId <= 0 || num <= 0) {
        throw new MyException("参数异常!");
      }
    } catch (Exception e) {
      res.failedFor302("参数异常!");
      return;
    }
    res.setResult(shopCatService.updateNum(recordId, num));
  }

  /**
   * 获取订单数量
   *
   * @param req
   * @param res
   * @throws IOException
   */
  public void getNum(MyHttpRequest req, MyHttpResponse res) throws IOException {
    res.success(shopCatService.getNum(req.getCurrentUser().getId()));
  }
}
